/**
 * 报表设计公司(2011)
 */
package net.abbp.report.model.cell.style.background;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;

import com.reportgear.report.core.api.compare.ComparatorUtils;
import com.reportgear.report.view.grid.util.GraphHelper;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class PatternBackground extends AbstractBackground {

	private int patternIndex;
	private Color foreground;
	private Color background;
	static byte[] staticByteArray = { -86, 85, -86, 85, -86, 85, -86, 85, -69, -18, -69, -18, -69, -18, -69, -18, 34,
			-120, 34, -120, 34, -120, 34, -120, 0, -1, -1, 0, 0, -1, -1, 0, 102, 102, 102, 102, 102, 102, 102, 102, 56,
			28, 14, 7, -125, -63, -32, 112, 7, 14, 28, 56, 112, -32, -63, -125, 102, 102, -103, -103, 102, 102, -103,
			-103, -103, -1, 102, -1, -103, -1, 102, -1, 0, -1, 0, 0, 0, -1, 0, 0, 68, 68, 68, 68, 68, 68, 68, 68, 17,
			-120, 68, 34, 17, -120, 68, 34, 34, 68, -120, 17, 34, 68, -120, 17, -120, -120, -120, -1, -120, -120, -120,
			-1, 68, -86, 17, -86, 68, -86, 17, -86, 0, 34, 0, -120, 0, 34, 0, -120, 0, -128, 0, 8, 0, -128, 0, 8, 0, 0,
			0, -128, 0, 0, 0, 8, 21, -86, 85, -86, 81, -86, 85, -86, -86, -35, -86, 119, -86, -35, -86, 119, -1, -18,
			-1, -69, -1, -18, -1, -69, -1, -3, -1, -33, -1, -3, -1, -33, -2, -1, -1, -1, -17, -1, -1, -1, -16, 15, 15,
			15, 15, -16, -16, -16, -128, -1, -128, -128, -128, -128, -128, -128, 0, -86, 0, 32, 0, 32, 0, 32, 16, -1,
			1, 1, 1, -1, 16, 16, 64, -128, 1, 3, -124, 72, 48, 32, -86, -86, -86, -86, -86, -86, -86, -86, 0, -1, 0,
			-1, 0, -1, 0, -1, 8, -128, -128, -128, -128, 8, 8, 8, 0, 0, 0, -16, 0, 0, 0, 15, -103, -52, 102, 51, -103,
			-52, 102, 51, 102, -52, -103, 51, 102, -52, -103, 51, 17, 0, 0, 0, 0, -120, 68, 34, 0, 0, 0, 17, 34, 68,
			-120, 0, 0, 16, 56, 124, -2, 124, 56, 16, 68, -126, 1, -126, 68, 40, 16, 40, 0, -120, 0, 2, 0, -120, 0, 32,
			-40, 27, 3, 48, -79, -115, 12, -64, 16, 2, 64, 8, -128, 4, 32, 1, 72, -124, 3, 48, 72, -124, 3, 48, 32, 64,
			32, 0, 4, 2, 4, 0, 24, 0, 3, -92, 24, 0, 3, -92, 12, 3, -128, 64, 64, -64, 33, 18, -126, 68, 42, 17, -118,
			68, -88, 17, 30, 85, -86, 85, -86, 30, 30, 30, 62, -35, 98, -29, -29, -35, 38, 62 };

	public PatternBackground() {
		this(0, Color.lightGray, Color.black);
	}

	public PatternBackground(int paramInt, Color paramColor1, Color paramColor2) {
		this.patternIndex = 0;
		this.foreground = null;
		this.background = null;
		setPatternIndex(paramInt);
		setForeground(paramColor1);
		setBackground(paramColor2);
	}

	private TexturePaint createPattern(int paramInt) {
		if ((paramInt < 0) || (paramInt > 47)) {
			paramInt = 0;
		}
		Color localColor1 = getForeground();
		if (localColor1 == null) {
			localColor1 = Color.lightGray;
		}
		Color localColor2 = getBackground();
		if (localColor2 == null) {
			localColor2 = Color.black;
		}
		int i = createColorInt(localColor1);
		int j = createColorInt(localColor2);
		BufferedImage localBufferedImage = new BufferedImage(8, 8, 1);
		int k = 8 * paramInt;
		for (int l = 0; l < 8; ++l) {
			int i1 = staticByteArray[(k + l)];
			int i2 = 7;
			for (int i3 = 0; i3 < 8; ++i3) {
				localBufferedImage.setRGB(i2--, l, ((i1 & 1 << i3) != 0) ? j : i);
			}
		}
		Rectangle localRectangle = new Rectangle(0, 0, 8, 8);
		return new TexturePaint(localBufferedImage, localRectangle);
	}

	private int createColorInt(Color paramColor) {
		return (paramColor.getRed() << 16 | paramColor.getGreen() << 8 | paramColor.getBlue());
	}

	public int getPatternIndex() {
		return this.patternIndex;
	}

	public void setPatternIndex(int paramInt) {
		this.patternIndex = paramInt;
	}

	public Color getForeground() {
		return this.foreground;
	}

	public void setForeground(Color paramColor) {
		this.foreground = paramColor;
	}

	public Color getBackground() {
		return this.background;
	}

	public void setBackground(Color paramColor) {
		this.background = paramColor;
	}

	public void paint(Graphics paramGraphics, Shape paramShape) {
		TexturePaint localTexturePaint = createPattern(getPatternIndex());
		if (localTexturePaint == null) {
			return;
		}
		Graphics2D localGraphics2D = (Graphics2D) paramGraphics;
		Paint localPaint = localGraphics2D.getPaint();
		localGraphics2D.setPaint(localTexturePaint);
		GraphHelper.fill(localGraphics2D, paramShape);
		localGraphics2D.setPaint(localPaint);
	}

	public boolean equals(Object paramObject) {
		if (!(paramObject instanceof PatternBackground)) {
			return false;
		}
		PatternBackground localPatternBackground = (PatternBackground) paramObject;
		return ((localPatternBackground.getPatternIndex() != getPatternIndex())
				|| (!(ComparatorUtils.equals(localPatternBackground.getForeground(), getForeground()))) || (!(ComparatorUtils
				.equals(localPatternBackground.getBackground(), getBackground()))));
	}

}
